package com.baitengsoft.patentapimonitor.api.controller;

import com.baitengsoft.patentapimonitor.api.model.MenuModel;
import com.baitengsoft.patentapimonitor.api.model.MenuViewModel;
import com.baitengsoft.patentapimonitor.api.model.WebRolesViewModel;
import com.baitengsoft.patentapimonitor.api.model.WebRolesmenuRlatViewModel;
import com.baitengsoft.patentapimonitor.api.model.param.EditRolesMenuRlatParam;
import com.baitengsoft.patentapimonitor.api.service.WebMenuService;
import com.baitengsoft.patentapimonitor.api.service.WebRolesService;
import com.baitengsoft.patentapimonitor.api.service.WebRolesmenuRlatService;
import com.baitengsoft.patentapimonitor.common.utils.PageResult;
import com.baitengsoft.patentapimonitor.common.utils.Res;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**rolesmanage
 * Created by Admin on 2017/12/19.
 */
@RestController
@RequestMapping("api/v1/admin")
@Api(value = "后台-样式管理",description = "样式管理")
public class AdminRolesManageController extends AbstractController{

    @Autowired
    private WebRolesService webRolesService;
    @Autowired
    private WebRolesmenuRlatService webRolesmenuRlatService;

    @Autowired
    private WebMenuService webmenuservice;
    /**
     * 获取样式管理列表
     * @param model
     * @return
     */
    @ApiOperation(value = "样式管理列表")
    @RequestMapping(value="/rolesmenu", method= RequestMethod.POST)
    public Res getRolesMenu(@RequestBody WebRolesViewModel model){

        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        try {
            PageResult<WebRolesmenuRlatViewModel> modelPageResult= webRolesService.getRoles(model);
            if(modelPageResult!=null){
                flag=true;
                map.put("data",modelPageResult);
            }else{
                map.put("msg","暂无数据");
            }
        } catch (Exception e) {
            map.put("msg","获取数据异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }


    /**
     * 新增/修改
     * @param
     * @param
     * @return
     */
    @ApiOperation(value = "编辑样式")
    @RequestMapping(value="/editrolesmenu", method= RequestMethod.POST)
    public Res editRolesMenu(@RequestBody EditRolesMenuRlatParam param){

        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        try {
            Integer userId=Integer.parseInt(getAdminUserId().toString());
            Integer successId=webRolesmenuRlatService.saveRolesMenuRlat(param.getRolesName(),param.getIds(),param.getId(),userId);
            if(successId!=0){
                flag=true;
                map.put("data",successId);
                map.put("msg","操作成功");
            }else {
                map.put("msg","操作失败");
            }
        } catch (NumberFormatException e) {
            map.put("msg","操作异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }



    @ApiOperation(value = "删除样式")
    @RequestMapping(value="/delrolesmenu", method= RequestMethod.GET)
    public Res delRolesMenu(@RequestParam("ids") String ids){

        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        try {
            Integer userId=Integer.parseInt(getAdminUserId().toString());//暂时不考虑登录问题
            Map<String,Object> resultMap=webRolesmenuRlatService.delRolesMenuRlat(ids,userId);
            int num=Integer.parseInt(String.valueOf(resultMap.get("num")));
            if(num==-1){//该样式正被平台使用
                map.put("msg","删除失败(有样式正在被使用)");
                map.put("data",resultMap.get("rolesName"));
            }
            if(num==0){
                map.put("msg","删除失败");
            }
            if(num==1) {
                flag=true;
                map.put("msg","删除成功");
            }
        } catch (NumberFormatException e) {
            map.put("msg","操作异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }


    @ApiOperation(value = "获取样式详情")
    @RequestMapping(value="/getrolesmenu", method= RequestMethod.GET)
    public Res getRolesMenu(@RequestParam("id") String id){

        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        try {
            MenuViewModel model=webRolesmenuRlatService.searchModelByRolesId(id);
            if(model==null){
                map.put("msg","暂无数据");
            }else {
                flag=true;
                map.put("data",model);
            }
        } catch (NumberFormatException e) {
            map.put("msg","操作异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }

    @ApiOperation(value = "获取所有菜单")
    @RequestMapping(value="/searchmenus", method= RequestMethod.GET)
    public Res searchMenus(){

        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        try {
            List<MenuModel> models=webmenuservice.search();
            if(models==null){
                map.put("msg","暂无数据");
            }else {
                flag=true;
                map.put("data",models);
            }
        } catch (NumberFormatException e) {
            map.put("msg","操作异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }

    @ApiOperation(value = "验证样式名称")
    @RequestMapping(value="/checkmenus", method= RequestMethod.GET)
    public Res checkMenus(@RequestParam("rolesName") String rolesName){

        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        try {
            Boolean resultFlag=webRolesService.checkRolesName(rolesName);
            if(resultFlag){
                flag=true;
                map.put("msg","该样式名称可用");
            }else {
                map.put("msg","该样式名称不可用");
            }
        } catch (NumberFormatException e) {
            map.put("msg","操作异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }
}
